unit USymbolTableTests;

interface

uses
  TestFrameWork,
  USymbolTable;

type

  TUSymbolTableTests = class(TTestCase)
  private
    FTable: TSymbolTable;
  protected
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure testGet;
    procedure testRedefine;
    procedure testNotDefined;
    procedure testIncDecLevel;
    procedure testNotUsed;
    procedure testDelete;
  end;

implementation
uses Steps, LanguageExceptions;

{ TUSymbolTableTests }

procedure TUSymbolTableTests.SetUp;
begin
  inherited;
  FTable := TSymbolTable.Create;
  FTable.Add('X1', TStep(10));
  FTable.Add('D5', TStep(15));
  FTable.Add('TR1234', TStep(20));

end;

procedure TUSymbolTableTests.TearDown;
begin
  inherited;
  FTable.Free;
end;

procedure TUSymbolTableTests.testDelete;
var
  ItWas: Boolean;
begin
  FTable.Delete('D5');
  ItWas:= False;
  try
    FTable.Get('D5');
  except
    on E: ESymbolTableError do ItWas := (E.Kind = ekNotDefined);
  end;
  Check(ItWas);
end;

procedure TUSymbolTableTests.testGet;
begin
  CheckEquals(10, Integer(FTable.Get('X1')));
  CheckEquals(15, Integer(FTable.Get('D5')));
  CheckEquals(20, Integer(FTable.Get('TR1234')));
end;

procedure TUSymbolTableTests.testIncDecLevel;
begin
  FTable.IncLevel;
  FTable.Add('a');
  FTable.Add('b', TStep(30));
  FTable.Use('a');
  FTable.Use('b');
  CheckEquals(30, Integer(FTable.Get('b')));
  FTable.DecLevel;

  FTable.IncLevel;
  FTable.Add('b', TStep(35));
  FTable.Add('c');
  CheckEquals(35, Integer(FTable.Get('b')));

  with TSymbolTable.Create do
  try
    IncLevel;
    Add('x');
    Use('x');
    DecLevel;
  finally
    Free;
  end;
end;

procedure TUSymbolTableTests.testNotDefined;
var
  ItWas: Boolean;
begin
  ItWas:= False;
  try
    FTable.Get('X100');
  except
    on E: ESymbolTableError do ItWas := (E.Kind = ekNotDefined);
  end;
  Check(ItWas);
  ItWas:= False;
  //case-sensivity
  FTable.Add('E');
  try
    FTable.Get('e');
  except
    on E: ESymbolTableError do ItWas := (E.Kind = ekNotDefined);
  end;
  Check(ItWas);
end;

procedure TUSymbolTableTests.testNotUsed;
var
  ItWas: Boolean;
begin
  ItWas:= False;
  FTable.IncLevel;
  FTable.Add('a');
  try
    FTable.DecLevel;
  except
    on E: ESymbolTableError do ItWas := (E.Kind = ekNotUsed);
  end;
  Check(ItWas);
end;

procedure TUSymbolTableTests.testRedefine;
var
  ItWas: Boolean;
begin
  ItWas:= False;
  try
    FTable.Add('D5', TStep(15));
  except
    on E: ESymbolTableError do ItWas := (E.Kind = ekRedefined);
  end;
  Check(ItWas);
end;

initialization

  TestFramework.RegisterTest('USymbolTableTests Suite',
    TUSymbolTableTests.Suite);
 
end.

